package com.opensky.qatest.ui.page;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.LoadableComponent;
import org.openqa.selenium.support.ui.Select;

import com.opensky.qatest.ui.component.OfferPageJoinModal;
import com.opensky.qatest.ui.component.OfferPageJoinNag;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.common.Properties;
import com.opensky.qatest.util.junit.Assert;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Offer page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.1
 * 
 */
public class OfferPage extends LoadableComponent<OfferPage> implements IPage {

    // ************************ Locators ************************

    private static final String HELP_LINK = "//div[@id='sky-bar']//a[@href='/content/faq']";

    private static final String LOGIN_LINK = "//div[@id='sky-bar']//div[@class='login']/a";

    private static final String TOP_NAV = "//ul[@id='category-nav']";

    private static final String BOTTOM_NAV = "//div[@class='wrap']/ul[@class='links']";

    private static String JOIN_4_ACCESS_BTN = "//form[@id='buy-box-form']//a[@class='button anon-follow']";

    private static String FOLLOW_ME_4_ACCESS_BTN = "//form[@id='buy-box-form']//input[@value='Follow Me for Access']";

    private static final String ADD2WISHLIST_BTN = "//div[@class='buy-box-body']//input[@name='wishlist']";

    private static final String RPODUCT_CHOISE_RADIO = "//form[@id='buy-box-form']//input[@type='radio']";

    private static final String RPODUCT_CHOISE_SELECT = "//form[@id='buy-box-form']//select";

    private static final String RPODUCT_CHOISE_LABEL = "//form[@id='buy-box-form']//span[@class='attribute select-attribute']//label[contains(@for, 'select_product')]";

    private String offerPageURL = Properties.get("env.root.url") + "%s/product/%s";

    // ************************ Complex page objects **************************

    private OfferPageJoinModal joinModal;

    private OfferPageJoinNag joinNag;

    private final WebDriverWrapper driver;

    public OfferPage(WebDriverWrapper driver, String curator, String product) {
	this.driver = driver;
	joinModal = new OfferPageJoinModal(driver);
	joinNag = new OfferPageJoinNag(driver);
	offerPageURL = String.format(offerPageURL, curator, product);
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(offerPageURL);
    }

    public void clickJoin4AccessBtn() {
	driver.waitAndClick("JOIN4ACCESS_BTN", By.xpath(JOIN_4_ACCESS_BTN));
    }

    public WishlistPage clickAdd2WishlistBtn() {

	WishlistPage wishlistPage = new WishlistPage(driver);

	driver.waitAndClick("ADD2WISHLIST_BTN", By.xpath(ADD2WISHLIST_BTN));

	if (isCoisePresent()) {
	    driver.waitAndClick("ADD2WISHLIST_BTN", By.xpath(ADD2WISHLIST_BTN));
	}

	return wishlistPage.isPageLoaded() ? wishlistPage : null;
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() {
	Assert.assertUrlEqual(offerPageURL, driver.getCurrentUrl());
    }

    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(offerPageURL);
    }

    public boolean isJoin4AccessBtnPresent() {
	return driver.waitAndVerifyElementPresent("JOIN_4_ACCESS_BTN", By.xpath(JOIN_4_ACCESS_BTN));
    }

    public boolean isFollowMe4AccessBtnPresent() {
	return driver.waitAndVerifyElementPresent("FOLLOW_ME_4_ACCESS_BTN", By.xpath(FOLLOW_ME_4_ACCESS_BTN));
    }

    public boolean isBaseNavElementsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("LOGIN_LINK", By.xpath(LOGIN_LINK)),
		driver.waitAndVerifyElementPresent("HELP_LINK", By.xpath(HELP_LINK)),
		driver.waitAndVerifyElementPresent("TOP_NAV", By.xpath(TOP_NAV)),
		driver.waitAndVerifyElementPresent("BOTTOM_NAV", By.xpath(BOTTOM_NAV)));
    }

    private boolean isCoisePresent() {

	boolean result = false;
	WebElement choise;

	if (driver.findElements(By.xpath(RPODUCT_CHOISE_RADIO)).size() != 0) {
	    choise = driver.findElement(By.xpath(RPODUCT_CHOISE_RADIO));
	    choise.click();
	    result = true;
	} else if (driver.findElements(By.xpath(RPODUCT_CHOISE_SELECT)).size() != 0) {
	    choise = driver.findElement(By.xpath(RPODUCT_CHOISE_SELECT));
	    Select sel = new Select(choise);
	    sel.selectByIndex(1);
	    result = true;
	} else if (driver.findElements(By.xpath(RPODUCT_CHOISE_LABEL)).size() != 0) {
	    choise = driver.findElement(By.xpath(RPODUCT_CHOISE_LABEL));
	    choise.click();
	    result = true;
	}

	return result;
    }

    // ************************ GET/SET methods **************************

    public OfferPageJoinModal getJoinModal() {
	return joinModal.isComponentLoaded() ? joinModal : null;
    }

    public OfferPageJoinNag getJoinNag() {
	return joinNag.isComponentLoaded() ? joinNag : null;
    }

    @Override
    public String getName() {
	return "Offer page";
    }
}
